package com.joeshing.security.service;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.joeshing.security.dao.AppLogDaoImpl;
import com.joeshing.security.model.AppLog;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class AppLogManagerImpl implements InitializingBean {

	@Resource
	private AppLogDaoImpl appLogDaoImpl;
	@Resource
	private SessionFactory sessionFactory;
	@Resource
	private CacheManager cacheManager;
	
	private final Logger logger = Logger.getLogger(this.getClass());  
	
	@Override
	public void afterPropertiesSet() throws Exception {
		logger.info("CurrentSession: " + sessionFactory == null ? "---------null--------" : "---------ok--------");
		// test();
		Collection<String> names = cacheManager.getCacheNames();
		for (String name : names) {
			System.out.println(name);
		}
	}

	// @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void test() {

		logger.info("start testing...");
		logger.info("Log Size: " + getAll().size());
		logger.info("new appLog...");
		AppLog appLog = new AppLog();
		appLogDaoImpl.insert(appLog);
		logger.info("save appLog...");
		logger.info("id=" + appLog.getId());
		logger.info("delete appLog...");
		appLogDaoImpl.delete(appLog);
		logger.info("end testing...");
	}

	public List<AppLog> getAll() {
		// return appLogDaoImpl.find("from " + AppLog.class.getName());
		return appLogDaoImpl.queryAll();
	}
}
